{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "DatabaseError",
     "evalue": "ORA-12541: TNS: 无监听程序",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mDatabaseError\u001b[0m                             Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m dsn \u001b[38;5;241m=\u001b[39m cx_Oracle\u001b[38;5;241m.\u001b[39mmakedsn(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlocalhost\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m1521\u001b[39m\u001b[38;5;124m'\u001b[39m, service_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mORCL\u001b[39m\u001b[38;5;124m'\u001b[39m)  \u001b[38;5;66;03m# 使用服务名\u001b[39;00m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;66;03m# 或者 dsn = cx_Oracle.makedsn('hostname', 'port', sid='SID')  # 使用SID\u001b[39;00m\n\u001b[1;32m----> 7\u001b[0m connection \u001b[38;5;241m=\u001b[39m \u001b[43mcx_Oracle\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mzxeps\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m111\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdsn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;66;03m# 执行一些操作，例如查询数据\u001b[39;00m\n\u001b[0;32m     10\u001b[0m cursor \u001b[38;5;241m=\u001b[39m connection\u001b[38;5;241m.\u001b[39mcursor()\n",
      "\u001b[1;31mDatabaseError\u001b[0m: ORA-12541: TNS: 无监听程序"
     ]
    }
   ],
   "source": [
    "import cx_Oracle\n",
    " \n",
    "# 数据库连接字符串格式：'username/password@hostname:port/service_name'\n",
    "# 或者 'username/password@hostname:port/SID'\n",
    "dsn = cx_Oracle.makedsn('localhost', '1521', service_name='ORCL')  # 使用服务名\n",
    "# 或者 dsn = cx_Oracle.makedsn('hostname', 'port', sid='SID')  # 使用SID\n",
    "connection = cx_Oracle.connect('zxeps', '111', dsn)\n",
    " \n",
    "# 执行一些操作，例如查询数据\n",
    "cursor = connection.cursor()\n",
    "cursor.execute('SELECT * FROM a_user')\n",
    "for row in cursor:\n",
    "    print(row)\n",
    " \n",
    "# 关闭游标和连接\n",
    "cursor.close()\n",
    "connection.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
